package cn.bdqn.mallmgr.tools;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author
 * @create 2022-07-12 9:13
 */
@Component
public class RedisTool {
    
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public void set(String key, String value, long time) {
        stringRedisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    public Boolean exists(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    public void expire(String key, long time) {
        stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
    }

    public void del(String key) {
        stringRedisTemplate.delete(key);
    }

    public void clear() {
        stringRedisTemplate.delete(Objects.requireNonNull(stringRedisTemplate.keys("*")));
    }
}
